package com.yuanyu.server.impl;

import com.yuanyu.common.util.AssertUtil;
import com.yuanyu.common.util.CopyUtil;
import com.yuanyu.mapper.mysqlMapper.Factory_test_report.Factory_test_report_1_Mapper;
import com.yuanyu.mapper.mysqlMapper.Factory_test_report.Factory_test_report_2_Mapper;
import com.yuanyu.mapper.mysqlMapper.Factory_test_report.Factory_test_report_Mapper;
import com.yuanyu.mapper.mysqlMapper.MainTable_Mapper;
import com.yuanyu.mapper.mysqlMapper.StaffMapper;
import com.yuanyu.model.dto.Factory_test_reportDTO;
import com.yuanyu.model.entity.Factory_test_report.Factory_test_report;
import com.yuanyu.model.entity.Factory_test_report.Factory_test_report_1;
import com.yuanyu.model.entity.Factory_test_report.Factory_test_report_2;
import com.yuanyu.model.entity.MainTable;
import com.yuanyu.model.vo.Factory_test_reportVO;
import com.yuanyu.server.ITestPart10Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Transactional
public class TestPart10ServerImpl implements ITestPart10Server {
    @Autowired
    private Factory_test_report_Mapper factoryTestReportMapper;
    @Autowired
    private Factory_test_report_1_Mapper factoryTestReport1Mapper;
    @Autowired
    private Factory_test_report_2_Mapper factoryTestReport2Mapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private MainTable_Mapper mainMapper;

    @Override
    public String test() {
        return "测试成功";
    }

    @Override
    public String test10(Factory_test_reportVO factoryTestReportVO) {
        AssertUtil.isNull(selByCase(factoryTestReportVO.getInput_Case()),"其他工序未完成");
        Factory_test_report test = factoryTestReportMapper.selTableTime(factoryTestReportVO.getInput_Case());
        AssertUtil.isNull(test,"Case已存在,如需修改内容请前往后台修改");

        Factory_test_report factoryTestReport = new Factory_test_report();
        Factory_test_report_1 factoryTestReport1 = new Factory_test_report_1();
        Factory_test_report_2 factoryTestReport2 = new Factory_test_report_2();

        CopyUtil.copyClass(factoryTestReportVO,factoryTestReport);
        factoryTestReport.setCreatedAt(LocalDateTime.now());
        factoryTestReport.setUpdatedAt(LocalDateTime.now());
        CopyUtil.copyClass(factoryTestReportVO,factoryTestReport1);
        CopyUtil.copyClass(factoryTestReportVO,factoryTestReport2);

        System.out.println(factoryTestReport);
        System.out.println(factoryTestReport1);
        System.out.println(factoryTestReport2);

        int i = factoryTestReportMapper.addTableTime(factoryTestReport);
        int i1 = factoryTestReport1Mapper.addTable1(factoryTestReport1);
        int i2 = factoryTestReport2Mapper.addTable2(factoryTestReport2);

        AssertUtil.isTrue(i == 1 && i1 == 1 && i2 == 1,"添加数据出现异常,请检查");

        int i3 = mainMapper.updateState(factoryTestReportVO.getInput_Case(), "1");
        AssertUtil.isTrue(i3 == 1 ,"完成订单出现问题");
        int i4 = mainMapper.updateNumberOfDownlines(factoryTestReportVO.getInput_Case());
        AssertUtil.isTrue(i4 == 1 ,"修改下线数量出现问题");
        mainMapper.updateByCaseFormEndTime(factoryTestReportVO.getInput_Case());
        return "添加成功";
    }

    @Override
    public String updateTable10(Factory_test_reportVO factoryTestReportVO) {

        Factory_test_report factoryTestReport = new Factory_test_report();
        Factory_test_report_1 factoryTestReport1 = new Factory_test_report_1();
        Factory_test_report_2 factoryTestReport2 = new Factory_test_report_2();

        CopyUtil.copyClass(factoryTestReportVO,factoryTestReport);
        factoryTestReport.setUpdatedAt(LocalDateTime.now());
        CopyUtil.copyClass(factoryTestReportVO,factoryTestReport1);
        CopyUtil.copyClass(factoryTestReportVO,factoryTestReport2);

        int i = factoryTestReportMapper.updateTableTime(factoryTestReport);
        int i1 = factoryTestReport1Mapper.updateTable1(factoryTestReport1);
        int i2 = factoryTestReport2Mapper.updateTable2(factoryTestReport2);

        AssertUtil.isTrue(i == 1 && i1 == 1 && i2 == 1,"表10修改数据出现异常,请检查");
        return "修改成功";
    }

    @Override
    public Factory_test_reportDTO getTest10(String Case) {
        Factory_test_report factoryTestReport = factoryTestReportMapper.selTableTime(Case);
        if (factoryTestReport == null)return null;
        Factory_test_report_1 factoryTestReport1 = factoryTestReport1Mapper.selTable1(Case);
        Factory_test_report_2 factoryTestReport2 = factoryTestReport2Mapper.selTable2(Case);

        Factory_test_reportDTO factoryTestReportDTO = new Factory_test_reportDTO();
        CopyUtil.copyClass(factoryTestReport,factoryTestReportDTO);
        CopyUtil.copyClass(factoryTestReport1,factoryTestReportDTO);
        CopyUtil.copyClass(factoryTestReport2,factoryTestReportDTO);
        return factoryTestReportDTO;
    }

    private MainTable selByCase(String Case) {
        return staffMapper.queryByCaseId(Case);
    }
}
